package com.luoran.bartalks.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Hyperlink;
/**
 * 
 * @ClassName: ExcelWriteImgUtil 
 * @Description:  Excel绘图工具类
 * @author: M1nG
 * @date: 2018年10月9日 下午2:31:30
 */
public class ExcelWriteImgUtil {

	/**
	 * 写入图片，并插入链接
	 * 
	 * @param cell
	 *            要插入链接的单元格位置
	 * @param sheetName
	 *            插入的图片所在的工作表
	 * @param patriarch
	 *            画图的顶级管理器，一个sheet只能获取一次，多次插入图片请使用同一个patriarch对象
	 * @param wb
	 *            HSSFWorkbook对象
	 * @param file
	 *            图片文件
	 * @param cellPoint
	 *            自定义的对象，指定要插入图片的坐标(x, y)
	 * @return cellPoint 自定义的对象，返回下一个要插入图片的坐标(x, y)
	 * @throws IOException
	 */
	public static CellPoint whiteImg(HSSFCell cell, String sheetName, HSSFPatriarch patriarch, HSSFWorkbook wb,
			File file, CellPoint cellPoint) throws IOException {
		Hyperlink hyperlink = new HSSFHyperlink(Hyperlink.LINK_DOCUMENT);
		// sheetName表示sheet页名称 "AXX"表示第几列第几行
		hyperlink.setAddress(sheetName + "!A" + (cellPoint.getY() + 1));
		cell.setHyperlink(hyperlink);

		/* 设置为超链接的样式 */
		HSSFCellStyle linkStyle = wb.createCellStyle();
		HSSFFont cellFont = wb.createFont();
		cellFont.setUnderline((byte) 1);
		cellFont.setColor(HSSFColor.BLUE.index);
		linkStyle.setFont(cellFont);
		cell.setCellStyle(linkStyle);

		BufferedImage bufferImg = null;
		ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
		bufferImg = ImageIO.read(file);
		ImageIO.write(bufferImg, "JPG", byteArrayOut);

		int x1 = cellPoint.getX();
		int y1 = cellPoint.getY();
		int width = bufferImg.getWidth() / 64;
		int height = bufferImg.getHeight() / 18;

		int x2 = x1 + width;
		int y2 = y1 + height;
		// anchor主要用于设置图片的属性
		HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) x1, y1, (short) x2, y2);
		// 插入图片
		patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
		cellPoint = new CellPoint(x1, y2 + 1);
		return cellPoint;
	}
	
	/**
	 * Excel sheetName 过滤非法字符
	 * 
	 * @param sheetName
	 * @return
	 */
	public static String sheetNameReplace(String sheetName) {
		if (sheetName != null && sheetName != "") {
			sheetName = sheetName.replace("/", "_").replace("\\", "_").replace("*", "_").replace("?", "_")
					.replace("[", "_").replace("]", "_").replace("-", "_");
		}
		while (sheetName.indexOf("__") != -1) {
			sheetName = sheetName.replace("__", "_");
		}
		return sheetName;
	}

}